package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.*;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.*;
import com.wisewin.circle.entity.dto.ActivitySignUserDTO;
import com.wisewin.circle.util.DateUtil;
import com.wisewin.circle.util.PageInfo;
import com.wisewin.circle.util.PageObjectUtil;
import com.wisewin.circle.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;

@Service
@Transactional
public class GroupService {

    @Autowired
    private GroupDAO groupDAO;
    @Autowired
    private UserAddressDAO userAddressDAO;
    @Autowired
    private GroupOrderDAO groupOrderDAO;
    @Autowired
    private GroupOrderAddressDAO groupOrderAddressDAO;
    @Autowired
    private GroupCommentDAO groupCommentDAO;
    @Autowired
    private GroupCommentHeartDAO groupCommentHeartDAO;

    // 删除订单
    public ResultDTO deleteGroupOrder(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 订单是否存在
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null || groupOrderOld.getOrderStatus() != GroupOrderDO.ORDER_STATUS_WFK){
            return ResultDTOBuilder.failure("1", "当前订单不存在或者状态有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), null);
        // 刪除订单
        groupOrderDAO.deleteGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        // 修改剩余数量
        Integer amountRemain = groupDO.getAmountRemain() + groupOrderOld.getAmount();
        groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        return ResultDTOBuilder.success(groupOrderDO.getOrderNum());
    }

    // 取消订单
    public ResultDTO cancleGroupOrder(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 订单是否存在
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null || groupOrderOld.getOrderStatus() != GroupOrderDO.ORDER_STATUS_WFK){
            return ResultDTOBuilder.failure("1", "当前订单不存在或者状态有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), null);
        // 取消订单
        groupOrderDAO.cancleGroupOrder(groupOrderDO.getOrderNum());
        // 修改剩余数量
        Integer amountRemain = groupDO.getAmountRemain() + groupOrderOld.getAmount();
        groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        return ResultDTOBuilder.success(groupOrderDO.getOrderNum());
    }

    // 确认拼团
    public ResultDTO confirmGroupOrder(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 确认订单是否存在
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null || groupOrderOld.getOrderStatus() != GroupOrderDO.ORDER_STATUS_DQR){
            return ResultDTOBuilder.failure("1", "当前订单不存在或者状态有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), null);
        // 是否创建人
        if(groupDO.getUserId() != groupOrderDO.getUserId()){
            return ResultDTOBuilder.failure("1", "拼团创建者才可以结束", null);
        }
        // 确认订单
        groupOrderDAO.confirmGroupOrder(groupOrderDO.getOrderNum());
        return ResultDTOBuilder.success(groupOrderDO.getOrderNum());
    }

    // 关闭订单
    public ResultDTO closeGroupOrder(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 确认订单是否存在
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null || groupOrderOld.getOrderStatus() != GroupOrderDO.ORDER_STATUS_DQR){
            return ResultDTOBuilder.failure("1", "当前订单不存在或者状态有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), null);
        // 是否创建人
        if(groupDO.getUserId() != groupOrderDO.getUserId()){
            return ResultDTOBuilder.failure("1", "拼团创建者才可以结束", null);
        }
        // 关闭订单
        groupOrderDAO.closeGroupOrder(groupOrderDO.getOrderNum());
        // 修改剩余数量
        Integer amountRemain = groupDO.getAmountRemain() + groupOrderOld.getAmount();
        groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        return ResultDTOBuilder.success(groupOrderDO.getOrderNum());
    }

    // 查询拼团列表
    public ResultDTO queryGroupList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("userName", queryParamBO.getUserName());
        map.put("title", queryParamBO.getTitle());
        map.put("createDate", queryParamBO.getCreateDate());
        map.put("beginTime", queryParamBO.getBeginTime());
        map.put("endTime", queryParamBO.getEndTime());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(groupDAO.queryGroupCount(map), groupDAO.queryGroupList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(GroupDO groupDO: (List<GroupDO>)pageData.getRecords()){
            String beginTime = DateUtil.formatDate(groupDO.getBeginTime(), "MM-dd HH:ss");
            String endTime = DateUtil.formatDate(groupDO.getEndTime(), "MM-dd HH:ss");
            groupDO.setBeginTimeStr(beginTime);
            groupDO.setEndTimeStr(endTime);
            if(!StringUtils.isEmpty(groupDO.getImageUrl())){
                groupDO.setLstImageUrl(Arrays.asList(groupDO.getImageUrl().split(",")));
            }
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询拼团参加人列表
    public ResultDTO queryGroupJoinList(QueryParamBO queryParamBO, PageUtil pageUtil){
        if(queryParamBO.getGroupId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("groupId", queryParamBO.getGroupId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(groupDAO.queryGroupJoinCount(map), groupDAO.queryGroupJoinList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(ActivitySignUserDTO activitySignUserDTO : (List<ActivitySignUserDTO>)pageData.getRecords()){
            // 是否关注
            Integer ifFollow = activitySignUserDTO.getId() == queryParamBO.getUserId() ? 1 : activitySignUserDTO.getIfFollow();
            activitySignUserDTO.setIfFollow(ifFollow);
            activitySignUserDTO.setIfCanCancle(activitySignUserDTO.getId() == queryParamBO.getUserId() ? 0 : 1);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询拼团详情
    public ResultDTO queryGroupDetail(Integer groupId, Integer loginUserId){
        if(groupId == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 判断动态是否存在
        GroupDO groupDO = groupDAO.queryGroupDetail(groupId);
        if(groupDO == null){
            return ResultDTOBuilder.failure("1", "拼团不存在", null);
        }
        // 处理拼团时间
        String beginTime = DateUtil.formatDate(groupDO.getBeginTime(), "MM-dd HH:ss");
        String endTime = DateUtil.formatDate(groupDO.getEndTime(), "MM-dd HH:ss");
        groupDO.setBeginTimeStr(beginTime);
        groupDO.setEndTimeStr(endTime);
        // 处理图片
        if(!StringUtils.isEmpty(groupDO.getImageUrl())){
            groupDO.setLstImageUrl(Arrays.asList(groupDO.getImageUrl().split(",")));
        }
        // 是否发布拼团用户
        Integer ifGroupUser = loginUserId.equals(groupDO.getUserId()) ? 1 : 0;
        groupDO.setIfGroupUser(ifGroupUser);
        // 查询参与人信息
        List<ActivitySignUserDTO> lstUser = groupDAO.queryGroupUserJoin(groupId);
        Map<String, Object> result = new HashMap<>();
        result.put("group", groupDO);
        result.put("lstUser", lstUser);
        return ResultDTOBuilder.success(result);
    }

    // 查询拼团评论列表
    public ResultDTO queryGroupCommentList(QueryParamBO queryParamBO){
        if(queryParamBO.getGroupId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 判断动态是否存在
        GroupDO groupDO = groupDAO.queryGroupById(queryParamBO.getGroupId(), null);
        if(groupDO == null){
            return ResultDTOBuilder.failure("1", "拼团不存在", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("groupId", queryParamBO.getGroupId());
        List<GroupCommentDO> lstComment = groupCommentDAO.queryGroupCommentList(map);
        if(!CollectionUtils.isEmpty(lstComment)){
            for(GroupCommentDO groupCommentDO : lstComment){
                // 处理时间
                String commentTime = DateUtil.convertTimeToFormat(groupCommentDO.getCreateTime().getTime());
                groupCommentDO.setCommentTime(commentTime);
            }
            // 处理数据
            GroupCommentNodeBuilder menuTree = new GroupCommentNodeBuilder(lstComment);
            lstComment = menuTree.builTree();
        }
        return ResultDTOBuilder.success(lstComment);
    }

    // 取消评论点赞
    public ResultDTO cancleGroupCommentHeart(GroupCommentHeartDO groupCommentHeartDO){
        if(groupCommentHeartDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        GroupCommentDO groupCommentDO = groupCommentDAO.queryGroupCommentById(groupCommentHeartDO.getCommentId());
        if(groupCommentDO == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 判断是否已经点赞
        GroupCommentHeartDO groupCommentHeartOld = groupCommentHeartDAO.queryGroupCommentHeartById(groupCommentHeartDO.getUserId(), groupCommentHeartDO.getCommentId());
        if(groupCommentHeartOld == null){
            return ResultDTOBuilder.failure("1", "未点赞", null);
        }
        // 删除点赞
        groupCommentHeartDAO.delete(groupCommentHeartDO.getUserId(), groupCommentHeartDO.getCommentId());
        // 修改点赞次数
        groupCommentDAO.cancleHeartNum(groupCommentHeartDO.getCommentId());
        return ResultDTOBuilder.success(null);
    }

    // 添加拼团评论点赞
    public ResultDTO addGroupCommentHeart(GroupCommentHeartDO groupCommentHeartDO){
        if(groupCommentHeartDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        GroupCommentDO groupCommentDO = groupCommentDAO.queryGroupCommentById(groupCommentHeartDO.getCommentId());
        if(groupCommentDO == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 判断是否已经点赞
        GroupCommentHeartDO groupCommentHeartOld = groupCommentHeartDAO.queryGroupCommentHeartById(groupCommentHeartDO.getUserId(), groupCommentHeartDO.getCommentId());
        if(groupCommentHeartOld != null){
            return ResultDTOBuilder.failure("1", "已点赞", null);
        }
        // 添加拼团点赞
        groupCommentHeartDAO.insert(groupCommentHeartDO);
        // 修改拼团点赞次数
        groupCommentDAO.updateHeartNum(groupCommentHeartDO.getCommentId());
        return ResultDTOBuilder.success(null);
    }

    // 删除拼团评论
    public ResultDTO deleteGroupComment(GroupCommentDO groupCommentDO){
        if(groupCommentDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        GroupCommentDO groupCommentOld = groupCommentDAO.queryGroupCommentById(groupCommentDO.getCommentId());
        if(groupCommentOld == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 删除评论
        groupCommentDAO.delete(groupCommentDO.getCommentId());
        // 修改评论次数
        groupDAO.cancleCommentNum(groupCommentOld.getGroupId());
        return ResultDTOBuilder.success(null);
    }

    // 添加拼团评论
    public ResultDTO addGroupComment(GroupCommentDO groupCommentDO){
        Integer parentId = groupCommentDO.getParentId() == null ? 0 : groupCommentDO.getParentId();
        groupCommentDO.setParentId(parentId);
        if(groupCommentDO.getGroupId() == null || groupCommentDO.getAcceptUserId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        if(StringUtils.isEmpty(groupCommentDO.getContent())){
            return ResultDTOBuilder.failure("1", "请填写评论内容", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupCommentDO.getGroupId(), null);
        if(groupDO == null){
            return ResultDTOBuilder.failure("1", "拼团不存在", null);
        }
        // 添加拼团评论信息
        groupCommentDAO.insert(groupCommentDO);
        // 添加拼团评论数量
        groupDAO.updateCommentNum(groupCommentDO.getGroupId());
        return ResultDTOBuilder.success(null);
    }

    // 查询我参与的拼团详情
    public ResultDTO queryUserGroupOrderDetail(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null){
            return ResultDTOBuilder.failure("1", "拼团不存在", null);
        }
        // 处理图片
        if(!StringUtils.isEmpty(groupOrderOld.getImageUrl())){
            groupOrderOld.setLstImageUrl(Arrays.asList(groupOrderOld.getImageUrl().split(",")));
        }
        // 处理拼团信息
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), null);
        // 处理图片
        if(groupDO != null && !StringUtils.isEmpty(groupDO.getImageUrl())){
            groupDO.setLstImageUrl(Arrays.asList(groupDO.getImageUrl().split(",")));
        }
        groupOrderOld.setGroupDO(groupDO);
        // 处理拼团地址信息
        GroupOrderAddressDO groupOrderAddressDO = groupOrderAddressDAO.queryByOrderNum(groupOrderDO.getOrderNum());
        groupOrderOld.setGroupOrderAddressDO(groupOrderAddressDO);
        return ResultDTOBuilder.success(groupOrderOld);
    }

    // 查询我的拼团我参与的
    public ResultDTO queryUserGroupJoinList(QueryParamBO queryParamBO, PageUtil pageUtil, Integer loginUserId){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(groupDAO.queryUserGroupJoinCount(map), groupDAO.queryUserGroupJoinList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(GroupDO groupDO : (List<GroupDO>)pageData.getRecords()){
            // 处理图片
            if(!StringUtils.isEmpty(groupDO.getImageUrl())){
                groupDO.setLstImageUrl(Arrays.asList(groupDO.getImageUrl().split(",")));
            }
            // 是否发布拼团用户
            Integer ifGroupUser = loginUserId.equals(groupDO.getUserId()) ? 1 : 0;
            groupDO.setIfGroupUser(ifGroupUser);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询我的拼团我发起的
    public ResultDTO queryUserGroupList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(groupDAO.queryUserGroupCount(map), groupDAO.queryUserGroupList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(GroupDO groupDO : (List<GroupDO>)pageData.getRecords()){
            // 处理图片
            if(!StringUtils.isEmpty(groupDO.getImageUrl())){
                groupDO.setLstImageUrl(Arrays.asList(groupDO.getImageUrl().split(",")));
            }
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 定时处理拼团是否结束
    public void dealGroupOrder(){
        // 查询时间结束的拼团
        List<GroupDO> lstGroup = groupDAO.queryNeedEndGroup();
        if(!CollectionUtils.isEmpty(lstGroup)){
            for(GroupDO groupDO : lstGroup){
                endGroup(groupDO);
            }
        }
    }

    // 结束拼团
    public ResultDTO endGroup(GroupDO groupDO){
        if(groupDO.getGroupId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询拼团
        GroupDO groupOld = groupDAO.queryGroupById(groupDO.getGroupId(), ActivityDO.STATUS_YFB);
        if(groupOld == null){
            return ResultDTOBuilder.failure("1", "当前拼团不存在或者已结束", null);
        }
        // 是否创建人
        if(groupOld.getUserId() != groupDO.getUserId()){
            return ResultDTOBuilder.failure("1", "拼团创建者才可以结束", null);
        }
        // 结束拼团
        groupDAO.updateStatus(groupDO.getGroupId(), ActivityDO.STATUS_YJS);
        // 查询未支付订单数量
        int count = groupOrderDAO.queryGroupOrderCountWfk(groupDO.getGroupId());
        // 未支付订单删除
        groupOrderDAO.deleteGroupOrder(groupDO.getGroupId());
        // 修改剩余数量
        Integer amountRemain = groupOld.getAmountRemain() + count;
        groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        return ResultDTOBuilder.success(groupDO);
    }

    // 修改订单地址
    public ResultDTO updateGroupOrderAddress(GroupOrderAddressDO groupOrderAddressDO){
        if(StringUtils.isEmpty(groupOrderAddressDO.getOrderNum())){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询是否存在
        GroupOrderAddressDO groupOrderAddressOld = groupOrderAddressDAO.queryByOrderNum(groupOrderAddressDO.getOrderNum());
        if(groupOrderAddressOld == null){
            return ResultDTOBuilder.failure("1", "订单地址信息不存在", null);
        }
        if(StringUtils.isEmpty(groupOrderAddressDO.getName())){
            return ResultDTOBuilder.failure("1", "请填写姓名", null);
        }
        if(StringUtils.isEmpty(groupOrderAddressDO.getGender())){
            return ResultDTOBuilder.failure("1", "请填写性别", null);
        }
        if(StringUtils.isEmpty(groupOrderAddressDO.getPhone())){
            return ResultDTOBuilder.failure("1", "请填写手机号码", null);
        }
        if(StringUtils.isEmpty(groupOrderAddressDO.getAddress())){
            return ResultDTOBuilder.failure("1", "请填写收货地", null);
        }
        if(StringUtils.isEmpty(groupOrderAddressDO.getAddressDetail())){
            return ResultDTOBuilder.failure("1", "请填写门牌号", null);
        }
        groupOrderAddressDO.setAddressIdChoose(groupOrderAddressOld.getAddressIdChoose());
        // 修改地址
        groupOrderAddressDAO.update(groupOrderAddressDO);
        // 同步用户地址
        UserAddressDO userAddressDO = new UserAddressDO();
        userAddressDO.setAddressId(groupOrderAddressOld.getAddressIdChoose());
        userAddressDO.setName(groupOrderAddressDO.getName());
        userAddressDO.setGender(groupOrderAddressDO.getGender());
        userAddressDO.setPhone(groupOrderAddressDO.getPhone());
        userAddressDO.setAddress(groupOrderAddressDO.getAddress());
        userAddressDO.setAddressDetail(groupOrderAddressDO.getAddressDetail());
        userAddressDAO.update(userAddressDO);
        return ResultDTOBuilder.success(groupOrderAddressDO);
    }

    // 重新支付拼团订单
    public ResultDTO payGroupOrder(GroupOrderDO groupOrderDO){
        if(StringUtils.isEmpty(groupOrderDO.getOrderNum()) || groupOrderDO.getAddressId() == null || groupOrderDO.getAmount() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        if(StringUtils.isEmpty(groupOrderDO.getImageUrl())){
            return ResultDTOBuilder.failure("1", "请上传付款截图", null);
        }
        // 查询订单
        GroupOrderDO groupOrderOld = groupOrderDAO.queryGroupOrderByOrderNum(groupOrderDO.getOrderNum());
        if(groupOrderOld == null || groupOrderOld.getOrderStatus() != GroupOrderDO.ORDER_STATUS_WFK){
            return ResultDTOBuilder.failure("1", "当前订单不存在或者状态有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderOld.getGroupId(), ActivityDO.STATUS_YFB);
        if(groupDO == null){
            return ResultDTOBuilder.failure("1", "当前拼团不存在或者已结束", null);
        }
        // 判断数量是否一致
        if(groupOrderDO.getAmount() != groupOrderOld.getAmount()){
            // 修改剩余数量
            Integer amountRemain = groupDO.getAmountRemain() + groupOrderOld.getAmount();
            groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
            if(groupOrderDO.getAmount() > amountRemain){
                return ResultDTOBuilder.failure("1", "数量大于剩余拼团数量", null);
            }
            // 修改剩余数量
            amountRemain = amountRemain - groupOrderDO.getAmount();
            groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        }
        // 总价格
        Double totalPrice = groupOrderDO.getAmount() * groupDO.getPrice();
        groupOrderDO.setTotalPrice(totalPrice);
        groupOrderDO.setGroupName(groupDO.getTitle());
        groupOrderDO.setPrice(groupDO.getPrice());
        // 修改订单状态
        groupOrderDAO.payGroupOrder(groupOrderDO);
        return ResultDTOBuilder.success(groupOrderDO.getOrderNum());
    }

    // 加入拼团
    public ResultDTO joinGroup(GroupOrderDO groupOrderDO){
        if(groupOrderDO.getGroupId() == null || groupOrderDO.getAddressId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询拼团
        GroupDO groupDO = groupDAO.queryGroupById(groupOrderDO.getGroupId(), ActivityDO.STATUS_YFB);
        if(groupDO == null){
            return ResultDTOBuilder.failure("1", "当前拼团不存在或者已结束", null);
        }
        // 查询地址
        UserAddressDO userAddressDO = userAddressDAO.queryUserAddressById(groupOrderDO.getAddressId());
        if(userAddressDO == null){
            return ResultDTOBuilder.failure("1", "收货地址不存在", null);
        }
        // 数量是否有误
        if(groupOrderDO.getAmount() == null){
            return ResultDTOBuilder.failure("1", "请选择拼团数量", null);
        }
        if(groupOrderDO.getAmount() > groupDO.getAmountRemain()){
            return ResultDTOBuilder.failure("1", "数量大于剩余拼团数量", null);
        }
        // 总价格
        Double totalPrice = groupOrderDO.getAmount() * groupDO.getPrice();
        groupOrderDO.setTotalPrice(totalPrice);
        groupOrderDO.setGroupName(groupDO.getTitle());
        groupOrderDO.setPrice(groupDO.getPrice());
        // 订单号
        String orderNum = DateUtil.getUUID();
        groupOrderDO.setOrderNum(orderNum);
        // 处理订单状态
        groupOrderDO.setOrderStatus(StringUtils.isEmpty(groupOrderDO.getImageUrl()) ? GroupOrderDO.ORDER_STATUS_WFK : GroupOrderDO.ORDER_STATUS_DQR);
        groupOrderDO.setPayTime(StringUtils.isEmpty(groupOrderDO.getImageUrl()) ? null : new Date());
        // 添加数据
        groupOrderDAO.insert(groupOrderDO);
        // 修改剩余数量
        Integer amountRemain = groupDO.getAmountRemain() - groupOrderDO.getAmount();
        groupDAO.updateAmountRemain(groupDO.getGroupId(), amountRemain);
        // 添加订单地址信息
        addGroupOrderAddress(orderNum, userAddressDO);
        return ResultDTOBuilder.success(groupOrderDO);
    }

    // 添加订单地址信息
    public void addGroupOrderAddress(String orderNum, UserAddressDO userAddressDO){
        GroupOrderAddressDO groupOrderAddressDO = new GroupOrderAddressDO();
        groupOrderAddressDO.setOrderNum(orderNum);
        groupOrderAddressDO.setName(userAddressDO.getName());
        groupOrderAddressDO.setGender(userAddressDO.getGender());
        groupOrderAddressDO.setPhone(userAddressDO.getPhone());
        groupOrderAddressDO.setAddress(userAddressDO.getAddress());
        groupOrderAddressDO.setAddressDetail(userAddressDO.getAddressDetail());
        groupOrderAddressDO.setAddressIdChoose(userAddressDO.getAddressId());
        // 添加数据
        groupOrderAddressDAO.insert(groupOrderAddressDO);
    }


    // 添加拼团
    public ResultDTO addGroup(GroupDO groupDO){
        if(StringUtils.isEmpty(groupDO.getTitle())){
            return ResultDTOBuilder.failure("1", "请输入标题", null);
        }
        if(StringUtils.isEmpty(groupDO.getBeginTimeStr())){
            return ResultDTOBuilder.failure("1", "请选择开始时间", null);
        }
        if(StringUtils.isEmpty(groupDO.getEndTimeStr())){
            return ResultDTOBuilder.failure("1", "请选择结束时间", null);
        }
        // 处理开始、结束时间
        Date beginTime = DateUtil.parseDate(groupDO.getBeginTimeStr(), "yyyy-MM-dd HH:mm");
        Date endTime = DateUtil.parseDate(groupDO.getEndTimeStr(), "yyyy-MM-dd HH:mm");
        if(endTime.getTime() < beginTime.getTime()){
            return ResultDTOBuilder.failure("1", "结束时间大于开始时间", null);
        }
        groupDO.setBeginTime(beginTime);
        groupDO.setEndTime(endTime);
        if(groupDO.getPrice() == null){
            return ResultDTOBuilder.failure("1", "请填写价格", null);
        }
        if(StringUtils.isEmpty(groupDO.getStandard())){
            return ResultDTOBuilder.failure("1", "请填写规格", null);
        }
        if(groupDO.getAmount() == null){
            return ResultDTOBuilder.failure("1", "请填写库存设定", null);
        }
        groupDO.setAmountRemain(groupDO.getAmount());
        if(StringUtils.isEmpty(groupDO.getIntroduce())){
            return ResultDTOBuilder.failure("1", "请填写拼团详情", null);
        }
        if(StringUtils.isEmpty(groupDO.getImageUrl())){
            return ResultDTOBuilder.failure("1", "请选择照片", null);
        }
        groupDO.setStatus(ActivityDO.STATUS_YFB);
        // 添加数据
        groupDAO.insert(groupDO);
        return ResultDTOBuilder.success(groupDO);
    }
}
